//https://leetcode.cn/problems/happy-number/description/

class Solution {
    //计算平方和
    int bitsum(int n)
    {
        int sum = 0;
        while (n)
        {
            int num = n % 10;
            sum += num * num;
            n /= 10;
        }

        return sum;
    }
public:
    bool isHappy(int n) {
        //快慢指针法(必有环)
        int slow = n;
        int fast = bitsum(n);
        while (slow != fast)
        {
            slow = bitsum(slow);
            fast = bitsum(bitsum(fast));
        }

        return slow == 1;


    }
};